package com.zenika.zenbinding.client.validator;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.zenika.zenbinding.client.exception.DataMessage;
import com.zenika.zenbinding.client.model.Model;

public class ValidationContext {
  
  private Set<Model> models;
  
  public ValidationContext() {
    this.models = new HashSet<Model>();
  }
  
  public void addModel(Model model) {
    this.models.add(model);
  }
  
  /**	 */
  public Set<DataMessage> getValidationState() {
    Set<DataMessage> result = new HashSet<DataMessage>();
    for ( Iterator<Model> iterator = this.models.iterator(); iterator.hasNext(); ) {
      Model currModel = iterator.next();
      DataMessage currMessage = currModel.getCurrentValidationState();
      if ( currMessage != null )
        result.add(currMessage);
    }
    return result;
  }
  
  /**	 */
  public Set<DataMessage> validate() {
    Set<DataMessage> result = new HashSet<DataMessage>();
    for ( Iterator<Model> iterator = this.models.iterator(); iterator.hasNext(); ) {
      Model currModel = iterator.next();
      DataMessage currMessage = currModel.validate();
      if ( currMessage != null )
        result.add(currMessage);
    }
    return result;
  }
  
}
